package com.sftz.modules.minapp.pay.module;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Param;

import com.sftz.framework.rersult.MobileResult;
import com.sftz.modules.minapp.pay.enums.WeixinPayChannelEnum;
import com.sftz.modules.minapp.pay.form.WxMpPayOrderForm;
import com.sftz.modules.minapp.pay.form.WxMpPayResultForm;
import com.sftz.modules.minapp.pay.service.WeappPayViewService;

/**
 * 微信小程序支付接口
 */
@IocBean
@At("/wx/pay")
public class WeappPayModule {

	private static final Log log = Logs.get();

	@Inject
	private WeappPayViewService weappPayViewService;

	/**
	 * 统一下单
	 */
	@At
	public Object unifiedOrder(@Param("..") WxMpPayOrderForm form) {
		return weappPayViewService.unifiedOrder(form);
	}

	/**
	 * 同步地址
	 */
	@At
	public Object paySuccess(@Param("..") final WxMpPayResultForm form) {
		try {
			//同步处理订单
			return weappPayViewService.paySuccess(form, WeixinPayChannelEnum.MP.intKey());
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("同步订单处理失败", null);
		}
	}

	/**
	 * 支付结果异步通知地址
	 */
	@At
	public void notify(final HttpServletRequest request, final HttpServletResponse response) {
		try {
			weappPayViewService.notify(request, response, WeixinPayChannelEnum.MP.intKey());
		} catch (Exception e) {
			log.info("异步通知接口调用异常");
			e.printStackTrace();
		}
	}

}
